package com.ymatou.iapi.trading.userquery.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.CollectionUtils;

import com.ymatou.iapi.trading.operate.parameter.BargainGroupService;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.req.BizVo;
import com.ymatou.iapi.trading.operate.parameter.req.ChangeOrderBizStatusBean;
import com.ymatou.iapi.trading.operate.service.ChangeOrderBizStatusCall;
import com.ymatou.iapi.trading.ordersync.service.SyncVerifyService;
import com.ymatou.iapi.trading.userquery.parameter.GetBuyerGroupOrderInfosBean;
import com.ymatou.iapi.trading.userquery.parameter.resp.GetBuyerGroupOrderInfosResp;
import com.ymatou.iapi.trading.userquery.service.GetBuyerGroupOrderInfosCall;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.PaymentProxyCallService;
import com.ymttest.common.entitie.C2COrderInfo.C2COrderInfoExt;
import com.ymttest.common.enums.BargainGroupStatusEnum;
import com.ymttest.common.enums.BargainGroupSubTypeEnum;
import com.ymttest.common.order.userquery.OrderInfoBuilder;
import com.ymttest.common.order.userquery.model.vo.BargainGroupInfo;
import com.ymttest.common.order.userquery.model.vo.OrderInfo;
import com.ymttest.database.model.mongo.query.Order;
import com.ymttest.database.mongo.OrderQueryWapper;
import com.ymttest.database.mongo.UserIndexQueryWapper;
import com.ymttest.database.mongo.config.UserIndexField;
import com.ymttest.database.mongo.converter.ConvertUtils;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;

/**
 *  根据主单号获取订单详情
 */
@TestSuite
public class Ts_GetBuyerGroupOrderInfos {

	private static GetBuyerGroupOrderInfosBean req;
	private static GetBuyerGroupOrderInfosCall getBuyerGroupOrderInfosCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile(" 根据主单号获取订单详情");
	}

	@Before
	public void caseUp() {
		req = new GetBuyerGroupOrderInfosBean();
		getBuyerGroupOrderInfosCall = new GetBuyerGroupOrderInfosCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getBuyerGroupOrderInfosCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void constructExpectedResult(GetBuyerGroupOrderInfosResp resp) {
		Query query = Query.query(Criteria.where(UserIndexField.USER_ID)
				.is(req.getUserId()).and(UserIndexField.MAIN_ORDER_ID)
				.is(req.getOrderGroupId()));
		List<Long> orderIdList = new UserIndexQueryWapper().queryOrderId(query,
				req.getUserId());
		List<Order> orderList = new OrderQueryWapper().findByIds(orderIdList);
		if (!CollectionUtils.isEmpty(orderList)) {
			orderList.forEach(o -> {
				ConvertUtils.convertBigDecimal(o);
				resp.getOrderInfos().add(
						OrderInfoBuilder.convertFromOrder(o, false));
			});
		}
	}

	private void verify() {
		GetBuyerGroupOrderInfosResp expectResponse = new GetBuyerGroupOrderInfosResp();
		constructExpectedResult(expectResponse);
		GetBuyerGroupOrderInfosResp actualResponse = getBuyerGroupOrderInfosCall
				.getBuyerGroupOrderInfosResp();

		List<OrderInfo> expectOrders = expectResponse.getOrderInfos();
		List<OrderInfo> actualOrders = actualResponse.getOrderInfos();

		for (int i = 0; i < expectOrders.size(); i++) {
			OrderInfo expect = expectOrders.get(i);
			OrderInfo actual = actualOrders.get(i);
			expect.verify(actual);

			if (actual != null) {
				BargainGroupInfo act = actual.getBargainGroup();
				BargainGroupInfo exp = new Ts_GetBuyerOrderInfosV2()
						.getBargainGroupInfoFromMssql((int) expect.getOrderId());
				if (exp == null) {
					DataUtil.verify(
							null,
							act,
							String.format(
									"验证 sqlserver中订单id:%d 没有参加砍价团(orderInfos.bargainGroup == null)",
									expect.getOrderId()));
				} else {
					DataUtil.verifyObject(exp, act,
							"砍价团信息orderInfos.bargainGroup");
				}
			}
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerGroupOrderInfos_001() {
		Logger.start(true, "订单状态为1 下单,根据主单号获取订单详情");
		try {
			OrderCreate orderCreate = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfoExt order = orderCreate.create1_M2cOrder(userId,
					sellerId);
			int tradingId = order.getiTradingId();
			int orderId = order.getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderGroupId(tradingId);

			String s = FormatUtil.beanToHttpString(req, true, true);

			getBuyerGroupOrderInfosCall.setData(s);
			getBuyerGroupOrderInfosCall.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerGroupOrderInfos_002() {
		Logger.start(true, "订单状态为2 付款,根据主单号获取订单详情");
		try {
			OrderCreate orderCreate = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfoExt order = orderCreate.create2_PayOrder(userId,
					sellerId);
			int tradingId = order.getiTradingId();
			int orderId = order.getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderGroupId(tradingId);

			String s = FormatUtil.beanToHttpString(req, true, true);
			getBuyerGroupOrderInfosCall.setData(s);
			getBuyerGroupOrderInfosCall.callService();

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerGroupOrderInfos_003() {
		Logger.start(true, "订单状态为3 取消订单,根据主单号获取订单详情");
		try {
			OrderCreate orderCreate = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfoExt order = orderCreate.create3_ShippedOrder(userId,
					sellerId);
			int tradingId = order.getiTradingId();
			int orderId = order.getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderGroupId(tradingId);

			String s = FormatUtil.beanToHttpString(req, true, true);

			getBuyerGroupOrderInfosCall.setData(s);
			getBuyerGroupOrderInfosCall.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerGroupOrderInfos_004() {
		Logger.start(true, "订单状态为 接单,根据主单号获取订单详情");
		try {
			OrderCreate orderCreate = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfoExt order = orderCreate.create17_AcceptOrder(userId,
					sellerId);
			int tradingId = order.getiTradingId();
			int orderId = order.getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderGroupId(tradingId);

			String s = FormatUtil.beanToHttpString(req, true, true);

			getBuyerGroupOrderInfosCall.setData(s);
			getBuyerGroupOrderInfosCall.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerGroupOrderInfos_005() {
		Logger.start(true, "订单状态为 发货,根据主单号获取订单详情");
		try {
			OrderCreate orderCreate = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfoExt order = orderCreate.create3_ShippedOrder(userId,
					sellerId);
			int tradingId = order.getiTradingId();
			int orderId = order.getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderGroupId(tradingId);

			String s = FormatUtil.beanToHttpString(req, true, true);

			getBuyerGroupOrderInfosCall.setData(s);
			getBuyerGroupOrderInfosCall.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerGroupOrderInfos_006() {
		Logger.start(true, "订单状态为 确认收货根据主单号获取订单详情");
		try {
			OrderCreate orderCreate = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfoExt order = orderCreate.create4_ReceivedOrder(userId,
					sellerId);
			int tradingId = order.getiTradingId();
			int orderId = order.getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderGroupId(tradingId);

			String s = FormatUtil.beanToHttpString(req, true, true);

			getBuyerGroupOrderInfosCall.setData(s);
			getBuyerGroupOrderInfosCall.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerGroupOrderInfos_007() {
		Logger.start(true, "订单状态为 申请退款根据主单号获取订单详情");
		try {
			OrderCreate orderCreate = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfoExt order = orderCreate.create4_ApplyRefundOrder(
					userId, sellerId);
			int tradingId = order.getiTradingId();
			int orderId = order.getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderGroupId(tradingId);

			String s = FormatUtil.beanToHttpString(req, true, true);

			getBuyerGroupOrderInfosCall.setData(s);
			getBuyerGroupOrderInfosCall.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerGroupOrderInfos_008() {
		Logger.start(true, "订单状态为 退款成功 根据主单号获取订单详情");
		try {
			OrderCreate orderCreate = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfoExt order = orderCreate.create4_RefundOrder(userId,
					sellerId);
			int tradingId = order.getiTradingId();
			int orderId = order.getiOrderId();
			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderGroupId(tradingId);

			String s = FormatUtil.beanToHttpString(req, true, true);

			getBuyerGroupOrderInfosCall.setData(s);
			getBuyerGroupOrderInfosCall.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerGroupOrderInfos_009() {
		Logger.start(true, "验证砍价团信息，查询成功, 团长开团 组团失败");
		try {
			PlaceOrderResp placeOrderResp = BargainGroupService.openGroup(null);
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			SyncVerifyService.getSyncResult(orderId);

			BizVo bizVo = new BizVo();
			bizVo.setOrderId(orderId);
			bizVo.setBizSubType(BargainGroupSubTypeEnum.BARGAINER.getValue()); //1：开团(团长)  2：参团(团员)
			bizVo.setBizStatus(BargainGroupStatusEnum.GRP_FAIL.getValue()); //砍价团状态(-3：组团失败 -2：参团失败 -1：开团失败  0：初始状态 1：开团成功 2：参团成功 3：组团成功)
			bizVo.setBizType(1);
			bizVo.setBizId(placeOrderResp.getBizId());

			List<BizVo> bizVos = new ArrayList<BizVo>();
			bizVos.add(bizVo);

			ChangeOrderBizStatusBean changeorderbizstatusBean = new ChangeOrderBizStatusBean();
			changeorderbizstatusBean.setBizVos(bizVos);
			ChangeOrderBizStatusCall changeorderbizstatusCall = new ChangeOrderBizStatusCall();
			changeorderbizstatusCall.setData(changeorderbizstatusBean);
			changeorderbizstatusCall.callService();

			Thread.sleep(2000l);

			req.setUserId(placeOrderResp.getUserId());
			req.setOrderGroupId(placeOrderResp.getTradingId());

			String s = FormatUtil.beanToHttpString(req, true, true);

			getBuyerGroupOrderInfosCall.setData(s);
			getBuyerGroupOrderInfosCall.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerGroupOrderInfos_010() {
		Logger.start(true, "订单状态为2  使用宝付支付付款,根据主单号获取订单详情");
		try {
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder5();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();

			int thirdTradingId = new OrderCallService().orderPayRequest(userId,
					BigDecimal.ZERO, tradingId, "13");

			PaymentProxyCallService.notifyCall("70",
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(), userId);

			SyncVerifyService.getSyncResult(orderId);

			req.setUserId(userId);
			req.setOrderGroupId(tradingId);

			String s = FormatUtil.beanToHttpString(req, true, true);
			getBuyerGroupOrderInfosCall.setData(s);
			getBuyerGroupOrderInfosCall.callService();

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
